// pages/waybill/waybill.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navindex: 0,
    According: false,
    freightnav: [
      { txt: '推送' ,val:0},
   
      { txt: '待装货',val:2 },
      { txt: '待卸货' ,val:3},
      { txt: '待支付' ,val:-1},
      { txt: '已完成',val:4 },

    ],
    waybill: [
    ],
    currentPage: 1,
    totalPage: 1,
    type: 0,
    multiArray:[],
    user_type:0
  },
  waybilldetal:function(e){
    var id = e.currentTarget.dataset.id;
    app.navTo('../wydealTrade/queren_zhuanghuo?id='+id);
  },
  initData: function () {
    // console.log(this.data.user_type)
    
    var that = this;
    var param = {
      type: that.data.type,
      currentpage: that.data.currentPage
    };
    
    wx.showLoading();
    util.commonRequest('orderList', 'post', param, function (res) {
    
      wx.hideLoading();
      var data = res.data;
      // console.log(data.data);
      if (data.code == 200) {
        var tempArr = data.data.data;
        var oriArr = that.data.waybill;
        var finalyArr = oriArr.concat(tempArr);
        that.setData({
          waybill: finalyArr,
          totalPage: data.data.last_page
        })
        if (finalyArr.length > 0) {
          that.setData({
            According: false
          })
        } else {
          that.setData({
            According: true
          })
        }
        // 
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  aggreeCancel:function(e){
    var id = e.currentTarget.dataset.id;
  },
  juCancel:function(e){
    var id = e.currentTarget.dataset.id;
  },
  confirmPay:function(e){
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认收款？',
      success(res) {
        if (res.confirm) {
          var param = {id:id};
          util.commonRequest('confirmOfflinePay', 'post', param, function (resw) {
            if (resw.data.code == 200) {
              that.setData({
                waybill: [],
                currentPage: 1
              })
              that.initData();
            } else {
              wx.showToast({
                icon: 'loading',
                title: resw.data.msg,
              })
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //推送接单
  goZh: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    var param = {
      id: id
    };
    wx.showLoading();
    util.commonRequest('pushAcceptOrder', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        wx.showToast({
          title: data.msg,
        })
        that.setData({
          waybill: [],
          currentPage: 1,
          type: 0
        })
        that.initData();
        // 
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  //推送拒绝
  defineZh: function (e) {
    var id = e.currentTarget.dataset.id;
    var id = e.currentTarget.dataset.id;
    var that = this;
    var param = {
      id: id
    };

    wx.showLoading();
    util.commonRequest('pushDefineOrder', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        wx.showToast({
          title: data.msg,
        })
        that.setData({
          waybill: [],
          currentPage: 1,
          type: 0
        })
        that.initData();
        // 
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  //运单状态
  wbillstate(e) {
    var id = e.currentTarget.dataset.id;
    app.navTo('../wbillstate/wbillstate?id=' + id)
  },
  //运单申请
  defaultapl(e) {
    var id = e.currentTarget.dataset.id;
    app.navTo('../defaultapl/defaultapl?id='+id)

  },
  // 取消订单
  delOrder: function (e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认取消订单？',
      success(res) {
        if (res.confirm) {

          var orderid = e.currentTarget.dataset.orderid
          var ordertid = e.currentTarget.dataset.ordertid
          var ordervehicles = e.currentTarget.dataset.ordervehicles
          var param={
            orderid: orderid,
            ordertid: ordertid,
            ordervehicles:ordervehicles
          };
          
          util.commonRequest('delorder', 'post', param, function (resw) {
            if (resw.data.code == 200) {
              var data = that.data.waybill;
              for (var i = 0; i < data.length;i++){
                if (data[i].id == orderid){
                  data.splice(i,1)
                }
              }
              that.setData({ waybill:data})
              wx.showToast({
                title: '取消订单成功',
                icon: 'success',
                duration: 1000
              })
            } else {
              wx.showToast({
                icon: 'loading',
                title: resw.data.msg,
              })
            }
          });



        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  avaliveDeliver:function(){
    var that = this;
    wx.showLoading();
    util.commonRequest('availableDeliver', 'post', {}, function (res) {
      
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        that.setData({
          multiArray:res.data.data
        })
        console.log(res.data.data)
        // 
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  //完成卸货
  completeXh: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '温馨提示:',
      content: '卸货请上传磅单！',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          wx.chooseImage({
            success(res) {
              // const tempFilePaths = res.tempFilePaths[0]
              that.uploadImage(res,id);
              // 
            }
          })
         
        }
      },
    })
  },
  uploadImage: function (res,id) {
    let file = res.tempFilePaths[0];
    var that = this;
    wx.uploadFile({
      //url: 'http://zdwl.4hl.cn/index.php/uploadImg',
      url: app.data.base_url + 'uploadImg',
      filePath: file,
      name: 'image',
      success: function (res) {
        var res = JSON.parse(res.data);
        if (res.code == 200) {
            that.xhFun(id, res.data);
        } else {
          wx.showToast({
            title: res.msg
          })
        }
      }
    })
  },

  bindMultiPickerChange:function(e){
    var val = e.detail.value;
    if(this.data.multiArray.length == 0){return}
    var id = this.data.multiArray[val]['id'];
    var oid = e.currentTarget.dataset.oid;
    var that = this;
    var param = {
      id: oid,//订单id
      deliver_id:id,//司机id
      deliver_vehicles:1//车辆数
    };
   
    wx.showLoading();
    util.commonRequest('capationDistributionOrder', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        that.setData({
          currentpage: 1,
          waybill: []
        })
        that.initData();
        that.avaliveDeliver();
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });

  },
  xhFun: function (id,xhimgUrl) {

    var that = this;
    var param = {
      id: id,
      xhimgUrl: xhimgUrl
    };
    wx.showLoading();
    util.commonRequest('confirmUnloadnt', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        that.setData({
          currentpage: 1,
          waybill: []
        })
        that.initData();

      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  //装货
  upload: function (e) {

    var id = e.currentTarget.dataset.id;
    var formid = e.detail.formId;
    this.collectFromId(formid);
    app.navTo('../upload/upload?id='+id)
  },
  collectFromId: function (formid) {
    var that = this;
    var param = {
      form_id: formid
    };
    util.commonRequest('formId', 'post', param, function (res) {

    });
  },
  //卸货
  uploadl: function (e) {
    var id = e.currentTarget.dataset.id;
    var formid = e.detail.formId;
    this.collectFromId(formid);
    app.navTo('../uploadl/upload?id=' + id)
  },
  // confirmCom: function (e) {
  //   var id = e.currentTarget.dataset.id;
  //   let that = this;
  //   // const tempFilePaths
  //   wx.showModal({
  //     title: '温馨提示:',
  //     content: '装货请上传磅单！',
  //     success: function (res) {
  //       if (res.cancel) {
  //         //点击取消,默认隐藏弹框
  //       } else {
  //         wx.chooseImage({
  //           success(res) {
  //           const  tempFilePaths = res.tempFilePaths[0] 
  //             that.zhFun(id, tempFilePaths);
  //         } 
  //        })
          
  //       }
  //     },
  //   }) 
  // },
  // //确认完成
  // zhFun: function (id, tempFilePaths) {

  //   var that = this;
  //   var param = {
  //     id: id,
  //     tempFilePaths: tempFilePaths
  //   };
  //   wx.showLoading();
  //   util.commonRequest('confirmShipment', 'post', param, function (res) {
  //     wx.hideLoading();
  //     var data = res.data;
  //     if (data.code == 200) {
       
  //       that.setData({
  //         currentpage: 1,
  //         waybill: []
  //       })
  //       that.initData();
 

  //     } else {
       
  //     }
  //   });
  // },
  //运单详情
  waybilldeil(e) {
    var id = e.currentTarget.dataset.id;
    app.navTo('../waybilldeil/waybilldeil?id=' + id)
  },

  click: function (e) { 
  
    var id = e.currentTarget.dataset.id; 
    var val = e.currentTarget.dataset.val;
    id = Number(id);
    if(id>0){
      id++;
      this.setData({
        navindex: id-1,
      })
    } else{
      this.setData({
        navindex: id,
      })
    }

    this.setData({
      // navindex: id-1,
      type: id,
      waybill: [],
      currentpage: 1,
      type:val

    })
    this.initData();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type;
     
    if (!type){
      type = 0;
    }
    if(type == 11){
      this.setData({ type: 2, navindex: 1 })
    } else if (type == 12){
      this.setData({ type: 3, navindex: 2 })
    } else if (type == -1) {
      this.setData({ type: -1, navindex: 3 })
    }
     else{
      this.setData({ type: type, navindex: type })
    }
 
    // this.initData();
    // console.log(this.data.user_type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    this.setData({
      waybill: [],
      currentpage: 1,
      
    })
    let that = this;
    util.commonRequest('getuserInfoByToken', 'post', {}, function (res) {
      if (res.data.code == 200) {
        
        that.setData({
          user_type: res.data.data.type
        })
      }
    });

    that.initData();
    var HQgoods = that.data.waybill;
    if (HQgoods.length == 0) {
      that.setData({
        According: true,
      })
    } else {
      that.setData({
        According: false,
      })
    }
 
    // that.avaliveDeliver();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currentpage = this.data.currentPage;
    var totalpage = this.data.totalPage;
    if (currentpage < totalpage) {
      this.setData({
        currentPage: currentpage + 1
      })
      this.initData();
    } else {
      console.log("已经是全部数据")
      // wx.showToast({
      //   'icon':'loading',
      //   title: '已经是全部数据了',
      // })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})